<?php
/**
 * Copyright (c) 2006-2008, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @link         http://feather-cm.googlecode.com/
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class Contact extends Model
{
	public $paranoia = false;
	
	function beforeValidate()
	{
		if (empty($this->validate))
		{
			$this->validate = array(
				'subject' => array(Valid::NotEmpty => _('Please entitle your message.')),
				'message' => array(
					Valid::NotEmpty    => _('You forgot to say something.'),
					'checkMessage'     => _("BBcode markup isn't allowed."),
					'checkMessageHTML' => _("HTML links aren't allowed.")
				),
				'name' => array('checkUserName' => _("You ain't an URL, are you?")),
				'email' => array(
					Valid::NotEmpty => _('Please provide your email so we may reply.'),
					Valid::Email    => _("Your email seems invalid.")
				)
			);
			
			# paranoia mode?
			if ($this->paranoia)
				$this->validate['message']['checkMessageLinks'] = _("Too many links for not much words! Are you a spammer?");
		}
		return true;
	}
	
	# A username can't be an URL
	function checkUserName($field, $value)
	{
		return preg_match('#^(http|ftp)[s]?://.+#', $value);
	}
	
	# Body can't contain [url=...]...[/url]
	function checkMessage($field, $value)
	{
		return preg_match('#\[url[=]?[^\]]*\][^\[]+\[/url\]#i', $value);
	}
	
	# Body can't contain <a href="...">
	function checkMessageHTML($field, $value)
	{
		return preg_match('#\<a[^>]*href=["\']?.+["\']?\>#i', $value);
	}
	
	# If there are more than 20% of external links in body (compared
	# to the total number of words), this is a spam.
	function checkMessageLinks($field, $value)
	{
		$value = preg_split('/\s/', $value, -1, PREG_SPLIT_NO_EMPTY);
		$words = count($value);
		$links = count(preg_grep('#((http|ftp|mailto)[s]?://[^\s]+?)#i', $value));
		return (100 / $words * $links > 20);
	}
	
	
	function & findAll($fields=null, $limit=null, $page=null, $recursive=null) {}
	function & find($fields=null, $recursive=null) {}
	
	protected function __save($type=null) { return true; }
	protected function __delete($id=null) { return true; }
}
?>